package com.bjb.vr.operation.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bjb.vr.common.ao.DeptUmsAccountAo;
import com.bjb.vr.common.ao.UmsUserRoleAo;
import com.bjb.vr.common.result.BaseErrorCode;
import com.bjb.vr.common.result.BaseResult;
import com.bjb.vr.common.result.CommonResult;
import com.bjb.vr.common.service.SysDeptService;
import com.bjb.vr.common.vo.DeptUmsAccountVo;
import com.bjb.vr.common.vo.DeptUmsRoleVo;
import com.bjb.vr.common.vo.UmsRoleVo;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: hujingbo
 * @create: 2022-08-24 09:59
 **/
@RestController
@RequestMapping("/api/operation/v1")
public class SysDeptUserController {

    @Resource
    private SysDeptService deptService;

    /**
     * 查询部门下用户列表
     *
     * @return
     */
    @PostMapping("/queryDeptUserList")
    public BaseResult queryDeptUserList(@RequestBody DeptUmsAccountAo deptUmsAccountAo) {
        IPage<DeptUmsAccountVo> deptUserList = deptService.queryDeptUserList(deptUmsAccountAo);
        return CommonResult.Ok().setResult(deptUserList);
    }

    /**
     * 查询角色信息
     *
     * @return
     */
    @PostMapping("/queryDeptRoleList")
    public CommonResult queryDeptRoleList() {
        List<DeptUmsRoleVo> umsRoleVos = deptService.queryDeptRoleList();
        return CommonResult.Ok().setResult(umsRoleVos);
    }

    /**
     * 新增用户
     *
     * @return
     */
    @PostMapping("/addDeptUser")
    public BaseResult addDeptUser(@RequestBody DeptUmsAccountAo deptUmsAccountAo) {
        // 新增用户
        boolean success = deptService.addDeptUser(deptUmsAccountAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_ADD);
        }
        return CommonResult.Ok();
    }

    /**
     * 编辑用户
     *
     * @return
     */
    @PostMapping("/updateDeptUser")
    public BaseResult updateDeptUser(@RequestBody DeptUmsAccountAo deptUmsAccountAo) {
        Assert.notNull(deptUmsAccountAo.getId(), "主键ID不能为空");
        // 编辑用户
        boolean success = deptService.updateDeptUser(deptUmsAccountAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_UPDATE);
        }
        return CommonResult.Ok();
    }

    /**
     * 删除用户
     *
     * @return
     */
    @PostMapping("/removeDeptUser")
    public BaseResult removeDeptUser(@RequestBody DeptUmsAccountAo deptUmsAccountAo) {
        Assert.notNull(deptUmsAccountAo.getId(), "主键ID不能为空");
        // 删除用户
        boolean success = deptService.removeDeptUser(deptUmsAccountAo);
        if (!success) {
            return CommonResult.Fail(BaseErrorCode.ERROR_REMOVE);
        }
        return CommonResult.Ok();
    }
}
